package com.xxh.market.utils;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

//工具类
public class jdbcUtils {
    private static String url="jdbc:mysql://localhost:3306/ykmall?serverTimezone=UTC&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8";
    private static String user="root";
    private static String password="root";
    private static String driver="com.mysql.cj.jdbc.Driver";
    //类被加载时就执行静态代码块,把配置文件的信息加载到类中。
    static {
        try {
//            Properties pro=new Properties();
//            pro.load(new FileReader("src/jdbc.properties"));
//            url=pro.getProperty("url");
//            user=pro.getProperty("user");
//            password=pro.getProperty("password");
//            driver=pro.getProperty("driver");
            Class.forName(driver);
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
    //获取连接
    public static Connection getConnection(){
        try {
            return DriverManager.getConnection(url,user,password);
        } catch (SQLException throwables)
        {
            throwables.printStackTrace();
            return null;
        }
    }
    //释放连接
    public  static void close(ResultSet rs, Statement stm, Connection conn){
        try {
            if(rs!=null){
                rs.close();
            }
            if(stm!=null){
                stm.close();
            }
            if(conn!=null){
                conn.close();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }


    }


}